package UI.TableModels;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import Logic.Account;

public class TableModelUsers extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9158572918823733184L;

	private ArrayList<Account> data = new ArrayList<Account>();
	private int usercount = 0;

	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount() {
		return usercount;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Account acc = data.get(rowIndex);
		Object returnvalue = 0;
		switch (columnIndex) {
		case 0:
			returnvalue = acc.getId();
			break;
		case 1:
			returnvalue = acc.getEmail();
			break;
		case 2:
			returnvalue = acc.isDeactivated();
			break;
		case 3:
			returnvalue = acc.getBalance();
			break;
		case 4:
			returnvalue = acc.getFirstname();
			break;
		case 5:
			returnvalue = acc.getLastname();
			break;
		}
		return returnvalue;
	}
	/**
	 * If we can find 16, throw 16 away and add fetched users to list and reprint, else print as many as possible. Return -1 for none.
	 * @param list
	 * The array list to process.
	 * @return
	 * The ID of the last user printed. -1 if no more to fetch.
	 */
	public int addData(ArrayList<Account> list) {
		if (list.isEmpty()) {
			return -1;
		} else {
			if (list.size() > 15) {
				list.remove(15);
				data.addAll(list);
				usercount = data.size();
				Account last = data.get(usercount - 1);
				fireTableDataChanged();
				return (int) last.getId();
			} else {
				data.addAll(list);
				usercount = data.size();
				fireTableDataChanged();
				return -1;
			}
		}
	}
	/**
	 * If we can find 16, throw 16 away and print, else print as many as possible. Return -1 for none.
	 * @param list
	 * The array list to process.
	 * @return
	 * The ID of the last user printed. -1 if no more to fetch.
	 */
	public int refreshData(ArrayList<Account> list) {

		if (list.isEmpty()) {
			return -1;
		} else {
			if (list.size() > 15) {
				list.remove(15);
				data = list;
				usercount = data.size();
				Account last = data.get(usercount - 1);
				fireTableDataChanged();
				return (int) last.getId();
			} else {
				data = list;
				usercount = data.size();
				fireTableDataChanged();
				return -1;
			}
		}
	}

	public void clearTable() {
		if (!data.isEmpty()) {
			data.clear();
			usercount = 0;
			fireTableDataChanged();
		}
	}

}
